<?php

namespace app\common\controller;

use think\Controller;
use think\Request;

class Home extends Controller
{
    public function __construct()
    {
        parent::__construct();

        // 调用用户模型
        $this->UserModel = model('User.User');

        $this->ConfigModel = model('Config');

        // 获取cookie
        $LoginUser = cookie('LoginUser');

        if(!empty($LoginUser))
        {
            $id = isset($LoginUser['id']) ? $LoginUser['id'] : 0;

            // 链表查询
            $User = $this->UserModel->with(['provinces','citys','districts'])->find($id);

            if(!$User)
            {
                // Cookie::delete()
                cookie('LoginUser',null);
                $this->error('你非法登录，请重新登录',url('home/index/login'));
                exit;
            }

            // 只要继承home公共控制器都会有$this->LoginUser属性
            $this->LoginUser = $User;

            // 继承home的所有控制器的视图会有这个LoginUser
            $this->assign('LoginUser',$User);

        }else {
            $this->error('请先登录',url('home/index/login'));
            exit;
        }

        $this->system();
    }

    public function system()
    {
        $SystemList = $this->ConfigModel->select();
        // halt($SystemList);

        // 定义一个空数组
        $System = [];

        foreach ($SystemList as $key => $item) {
            $System[$item['key']] = $item['value'];
        }

        // 赋值给所有的视图
        $this->assign('System',$System);
    }
}